﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Text.RegularExpressions;
using TEL.ClientClasses;
using TEL.ClientClasses.SipInterfaces;
using System.Windows.Forms;
using TEL.TelPhoneF.InfoList;

namespace TEL.TelPhoneF.Controls
{
    public partial class InfoPanel : CustomPanel, IInfoPanel
    {
        private object mLock = new object();
        public InfoPanel()
        {
            InitializeComponent();
            activeCall.Text = string.Empty;
            activeName.Text = string.Empty;

            foreach (Control c in this.Controls)
            {
                c.KeyDown += KeyDownEvent;
            }
            
        }

        public event EventHandler pictureMissClick;
        
        enum activeCallStateEnum
        {
            Work, Temp
        }

        private activeCallStateEnum activeCallState = activeCallStateEnum.Work;
        #region Implementation of IInfoPanel

       public string RegistrationData
        {
            get { return labRegistrat.Text; }
            // set { labRegistrat.Text = value; }
        }
       
        public void SetLineCall(string phone, string displayName)
        {

            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          activeCall.ForeColor = Color.White;
                                                          activeName.ForeColor = Color.White;
                                                          activeCallState = activeCallStateEnum.Work;
                                                          if (System.Text.RegularExpressions.Regex.IsMatch(phone, "@",
                                                                                                           RegexOptions
                                                                                                               .
                                                                                                               IgnoreCase))
                                                              activeCall.Text =
                                                                  ClientClasses.SipVizard.GetNumberFromSip(phone);
                                                          else activeCall.Text = phone;
                                                          activeName.Text = displayName;
                                                      }), null);
                }
                catch (Exception)
                {
                }
            }
        }

        public void ClearTempContact()
        {
            lock (mLock)
            {
                activeCall.ForeColor = Color.White;
                activeName.ForeColor = Color.White;

                activeCall.Text = string.Empty;
                activeName.Text = string.Empty;
            }
        }

        public void SetEnterText(string value)
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          labNumber.Text = value;
                                                      }), null);
                }
                catch (Exception)
                {
                }
            }
        }

        public event EventHandler<StringEventArgs> ContactCall;

        private bool m_microphone = true;
        public bool MicrophoneActive
        {
            get { return m_microphone; }
            set
            {
                if(m_microphone != value)
                {
                    m_microphone = value;
                    if (MicrophoneChecked != null) MicrophoneChecked(this, new BoolEventArgs(m_microphone));
                }
            }
        }
        public event EventHandler<BoolEventArgs> MicrophoneChecked;

        public void SetTempContact(Contact contact)
        {
            if (contact == null)
            {
                return;
            }
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          activeCall.ForeColor = Color.Yellow;
                                                          activeName.ForeColor = Color.Yellow;
                                                          activeCallState = activeCallStateEnum.Temp;

                                                          activeCall.Text = contact.ContactPhone;
                                                          activeName.Text = contact.ContactName;
                                                      }), null);
                }
                catch (Exception)
                {
                }
            }
        }

        public void SetLabelError(string message)
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          labelError.Text = message;
                                                      }), null);
                }
                catch (Exception)
                {
                }
            }
        }

        public void SetMissing(bool view)
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          pictureMiss.Visible = view;
                                                      }), null);
                }
                catch (Exception)
                {
                }
            }
        }

        public void SetRegistrationData(string value)
        {
            lock (mLock)
            {
                try
                {
                    this.Invoke(new MethodInvoker(delegate()
                                                      {
                                                          if (System.Text.RegularExpressions.Regex.IsMatch(value, "@",
                                                                                                           RegexOptions
                                                                                                               .
                                                                                                               IgnoreCase))
                                                              labRegistrat.Text =
                                                                  ClientClasses.SipVizard.GetNumberFromSip(value);
                                                          else labRegistrat.Text = value;
                                                          ELog.SaveToEventLog(labRegistrat.Text,
                                                                              EventLogEntryType.Information);
                                                      }), null);
                }
                catch (Exception)
                {
                }
            }
        }

        public event EventHandler<StringEventArgs> RegexStrChanged;
        private string m_regex = string.Empty;
        public string RegexStr
        {
            get { return m_regex; }
            set
            {
                m_regex = value;
                if(RegexStrChanged != null) RegexStrChanged(this, new StringEventArgs(m_regex));
            }
        }

       #endregion

        private void ClickContact(object sender, EventArgs e)
        {
            if (activeCallState == activeCallStateEnum.Temp)
            {
                if (ContactCall != null) ContactCall(this, new StringEventArgs(activeCall.Text));
            }
        }

        private void pictureMiss_Click(object sender, EventArgs e)
        {
            if (pictureMissClick != null) pictureMissClick(sender, e);
        }

        private void pictureMicrophone_Click(object sender, EventArgs e)
        {
            MicrophoneActive = !MicrophoneActive;
        }

        public bool BtnPanel_KeyDown(KeyEventArgs e)
        {
            bool res = false;
            if (e.Alt || e.Control || e.Shift) return false;
            switch (e.KeyCode)
            {
                case Keys.M:
                    pictureMicrophone_Click(this, EventArgs.Empty);
                    res = true;
                    break;
            }
            return res;
        }
    }
}